﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;

namespace NewWords
{
    public partial class WordList : Form
    {
        public WordList()
        {
            InitializeComponent();
        }

        public void RefreshData()
        {
            //Populate gridview
            DataTable words = new DataTable();
            try
            {
                SQLiteDataAdapter ad = new SQLiteDataAdapter("select spell, phonetic, explanation, sample, comment from word", new SQLiteConnection(DbHelper.SQLiteHelper.connectionString));
                ad.Fill(words);
            }
            catch (SQLiteException sqle)
            {
                MessageBox.Show("SQLiteException : " + sqle.Message);
                return;
            }
            this.bindingSource1.DataSource = words;
            this.dataGridView1.DataSource = this.bindingSource1;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Populate gridview
            RefreshData();

            this.dataGridView1.SelectionChanged += new EventHandler(dataGridView1_SelectionChanged);

            //Group textboxs
            this.groupBox1.Controls.Add(this.textBoxExplanation);
            this.groupBox1.Controls.Add(this.textBoxPhonetic);
            this.groupBox1.Controls.Add(this.textBoxSample);
            this.groupBox1.Controls.Add(this.textBoxSpell);
        }

        void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            DataGridView gridView = sender as DataGridView;
            if (gridView.SelectedRows.Count>0)
            {
                this.textBoxSpell.Text = gridView.SelectedRows[0].Cells["spell"].Value.ToString();
                this.textBoxSample.Text = gridView.SelectedRows[0].Cells["sample"].Value.ToString();
                this.textBoxPhonetic.Text = gridView.SelectedRows[0].Cells["phonetic"].Value.ToString();
                this.textBoxExplanation.Text = gridView.SelectedRows[0].Cells["explanation"].Value.ToString();
            }
        }

        private void buttonNewRow_Click(object sender, EventArgs e)
        {
            NewWordDialog dlg = new NewWordDialog();
            //dlg.Modal = true;
            if (DialogResult.OK == dlg.ShowDialog())
            {
                this.RefreshData();
            }
        }

        private void dataGridView1_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e)
        {
            if (!((DataGridView)sender).Rows[e.RowIndex].Selected)
            {
                foreach (DataGridViewRow row in ((DataGridView)sender).SelectedRows)
                {
                    row.Selected = false;
                }
                ((DataGridView)sender).Rows[e.RowIndex].Selected = true;
            }
            e.ContextMenuStrip = this.contextMenuStrip1;
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {

        }
    }
}
